<?php
namespace Admin\Controller;
use Think\Controller;
class SiteMapController extends BaseController {
	public function index(){
		//$this->display();
	}

	/**
	 * 生成百度Sitemap
	 */
	public function baidu() {
		$this->make('baidu');
	}

	/**
	 * 生成谷歌Sitemap
	 */
	public function google() {
		$this->make('google');
	}

	/**
	 * 生成地图文件
	 */
	public function make($target='') {
		// 参数
		$id = I("get.id", 0);
		$per = I("get.per", 500);
		$page = I("get.page", 1);

		$per = min(50000, $per); // 百度限制单文件5w条记录
		$per = max(500, $per); // 最少500条
		$page = max(1, $page);

		// 保存位置
		$filename = "/{$target}_sitemap_{$page}.xml";
		$path = ROOT . '/SiteMap';

		$model = D("Hash");
		$where = array(
			"id"	=> array("gt", $id),
		);
		$order['id'] = "ASC";
		$field = 'id,hash,time';
		$list = $model->field($field)->where($where)->order($order)->page($page,$per)->select();
		//$count = $model->where($where)->page($page,$per)->count();

		$this->assign("list", $list);
		$this->buildHtml($filename, $path, $target);

		// 更新地图索引
		if ($count = $per ) {
			// 记录更新时间
			$this->index['id'] = $page;
			$this->index['time'] = time();
		} elseif($count < $per ) {
			// 写入文件
			var_dump("end");
		}
		
	}
}